<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/3/31
 * Time: 11:04
 */

namespace app\home\controller;


class Ajax extends \think\Controller
{
    private $userInfo;
    public function _initialize(){
        parent::_initialize();
        $this->userInfo = cookie('userInfo');
    }
    //沙盘信息
    public function ajaxGetShaPan(){
        $id = input('get.id/d',0);
        $return['status'] = 0;
        if($id){
            $info = model('house_property')->where(['id'=>$id,'status'=>1])->find();
            if($info){
                $house_type = model('house_type')->field('id,house_id,jianzhu_area,title,room,ting,wei,chu,yang')->where(['id'=>['in',$info['house_type_id']],'status'=>1])->select();
                if($house_type){
                    foreach($house_type as &$v){
                        $v['url'] = url('Loupan/typeShow',['house_id'=>$v['house_id'],'id'=>$v['id']]);
                    }
                }
                $info['list'] = $house_type;
            }
            $return['status'] = 1;
            $return['data']   = $info;
        }
        return json($return);
    }
    //提交问题
    public function question(){
        $data             = input('post.');
        $return['status'] = 0;
        $return['msg']    = '提交失败';
        $userInfo         = $this->userInfo;
        if($data){
            $site = cache('site');
            if($userInfo){
                $data['user_id']   = $userInfo['id'];
                if(!isset($data['user_name'])){$data['user_name'] = $userInfo['nickname'];}
            }
            if($site['checkask'] == 1){
                $data['status'] = 0;
            }
            $cityInfo = cookie('cityInfo');
            $data['city'] = (!empty($cityInfo) && isset($cityInfo['id'])) ? $cityInfo['id'] : 0;
            if(model('ask')->allowField(true)->save($data)){
                if(isset($data['house_id'])){
                    model('house')->where(['id'=>$data['house_id']])->setInc('ask');
                }
                $return['status'] = 1;
                $return['msg']    = '问题提交成功';
            }
        }
        return json($return);
    }
    //收藏 中意
    public function italy(){
        $userInfo         = $this->userInfo;
        $return['status'] = 0;
        if(!$userInfo){
            $return['msg'] = '请先登录后再收藏';
        }else{
            $house_id = input('get.id/d',0);
            $type     = input('param.type/d',1);//1收藏 2中意
            $data['house_id'] = $house_id;
            $data['type']     = $type;
            $data['user_id']  = $userInfo['id'];
            $field = $type == 1 ? 'collection' : 'love';
            $obj = model('collection');
            if($obj->where(['user_id'=>$userInfo['id'],'house_id'=>$house_id,'type'=>$type])->count()==0){
                if($obj->allowField(true)->save($data)){
                    model('house')->where(['id'=>$house_id])->setInc($field);
                    $return['status'] = 1;
                    $return['msg'] = '收藏成功';
                }else{
                    $return['msg'] = '收藏失败';
                }
            }else{
                $return['msg'] = '您已收藏过该楼盘了';
            }
        }
        return json($return);
    }

    //提交购房需求
    public function demand(){
        if(request()->isAjax()){
            $data = input('post.');
            $return['status'] = 0;
            if(isset($data['code']) && !captcha_check($data['code'])){
                $return['msg']    = '验证码不正确';
            }else{
                if(model('demand')->allowField(true)->save($data)){
                    $return['status'] = 1;
                    $return['msg']    = '提交成功，稍候置业顾问会和你联系！';
                }else{
                    $return['msg']    = '提交失败';
                }
            }
            return json($return);
        }
    }
    //回答
    public function answer(){
        $return['status'] = 0;
        if(!$this->userInfo){
            $return['msg'] = '请登录后再回答';
        }else{
            $data['ask_id'] = input('post.ask_id/d',0);
            $data['content'] = input('post.content');
            $data['user_id'] = $this->userInfo['id'];
            $data['user_name'] = $this->userInfo['nickname'];
            $data['status'] = cache('site')['checkanswer'] == 1 ? 0 : 1;
            if(empty($data['ask_id'])){
                $return['msg'] = '参数错误';
            }else{
                if(model('answer')->save($data)){
                    model('ask')->save(['answer'=>1],['id'=>$data['ask_id']]);
                    $return['status'] = 1;
                    $return['msg']    = '感谢您的回答！';
                }else{
                    $return['msg']    = '提交失败';
                }
            }
        }
        return $return;
    }
    public function fabulous(){
        $id   = input('post.id/d',0);
        $type = input('post.type');
        $arr  = ['good','bad'];
        $return['status'] = 0;
        if($id){
            $fabulous = cookie('fabulous');
            if($fabulous == $id){
                $return['msg'] = '您已经表过态了';
            }else{
                if(!in_array($type,$arr)){
                    $return['msg'] = '类型错误';
                }else{
                    if(model('answer')->where(['id'=>$id])->setInc($type)){
                        $return['status'] = 1;
                        $return['msg']    = '谢谢支持！';
                        cookie('fabulous',$id);
                    }else{
                        $return['msg'] = '表态失败！';
                    }
                }
            }

        }else{
            $return['msg'] = '参数错误';
        }
        return json($return);
    }
    public function baoming(){
        $house_id = input('param.id/d',0);
        $type     = input('param.type/d',1);
        $this->assign('house_id',$house_id);
        $this->assign('type',$type);
        $this->assign('site',cache('site'));
        return $this->fetch();
    }
    public function tuangou(){
        $house_id = input('param.id/d',0);
        $type     = input('param.type/d',1);
        $this->assign('house_id',$house_id);
        $this->assign('type',$type);
        $this->assign('site',cache('site'));
        return $this->fetch();
    }
    //看房报名
    public function addOrder(){
        $data = input('post.');
        $return['status'] = 0;
        if(!isset($data['house_id'])){
            $return['msg'] = '参数错误';
        }else{
            if(!is_mobile($data['phone'])){
                $return['msg'] = '请填写正确的手机号码！';
            }else{
                if(isset($data['model']) && !empty($data['house_id'])){
                    $field = 'title';
                    if($data['model']=='second_house' || $data['model']=='rental'){
                        $field .= ',user_id';
                    }
                    $info = model($data['model'])->where(['id'=>$data['house_id']])->field($field)->find();
                    $data['house_title'] = $info['title'];
                    $data['uid']         = isset($info['user_id']) ? $info['user_id'] : 0;
                }else{
                    $data['house_title'] = '';
                }
                if($data['type'] == 4){
                    model('house_group')->where(['house_id'=>$data['house_id']])->setInc('buy_num');
                }
                $this->userInfo && $data['user_id'] = $this->userInfo['id'];
                if(model('baoming')->allowField(true)->save($data)){
                    $return['status'] = 1;
                    $return['msg']    = '提交成功！';
                    $return['code']=$this->sendToWeixin($data['name'],$data['phone'], $data['house_title'],$data['type']);
                }else{
                    $return['msg'] = '提交失败';
                }
            }
        }
        return json($return);
    }
    //二手房预约
    public function usedBook(){
        $house_id = input('param.id/d',0);
        $type     = input('param.type/d',1);
        $this->assign('house_id',$house_id);
        $this->assign('type',$type);
        $this->assign('site',cache('site'));
        return $this->fetch();
    }
    //出租房预约
    public function rentalBook(){
        $house_id = input('param.id/d',0);
        $type     = input('param.type/d',1);
        $this->assign('house_id',$house_id);
        $this->assign('type',$type);
        $this->assign('site',cache('site'));
        return $this->fetch();
    }
    /**
     * 发送微信消息 到指定微信号
     * 此功能需绑定微信号才可用
     */
    private function sendToWeixin($nickname,$mobile,$title,$msgtype=1){
        if(getWeixinConfig()){
            //发送客服消息
            $text = '用户-'.$nickname.'['.$mobile.']提交了'.baomingType($msgtype).'信息['.$title.']，请尽快联系他(她)吧！';

            $data['towxname'] = cache('weixin')['weixin'];
            $data['msgtype'] = 'text';
            $data['text'] = ['content'=>$text];
            vendor('zoujingli.wechat-php-sdk.include');
            $wechat = & \Wechat\Loader::get_instance('Receive',getWeixinConfig());
            if($return = $wechat->previewMassMessage($data)){
                return $return;
            }else{
                return 'error';
            }

        }
        return false;
    }
    //新房智能搜索提示
    public function autoHouseComplate(){
        $cityInfo = cookie('cityInfo');
        $city     = ($cityInfo && isset($cityInfo['id'])) ? $cityInfo['id'] : 0;
        $keyword  = input('get.keyword');
        $where['city']  = $city;
        $keyword && $where['title'] = ['like',['%'.$keyword.'%']];
        $lists = model('house')->where($where)->field('id,title,average_price as price,address')->limit(10)->select();
        $return['status'] = 0;
        $lists = objToArray($lists);
        if($lists){
            foreach($lists as &$v){
                $v['url'] = url('Loupan/detail',['id'=>$v['id']]);
            }
            $return['status'] = 1;
            $return['data']   = $lists;
        }
        return json($return);
    }
    //二手房智能搜索提示
    public function autoSecondComplate(){
        $cityInfo = cookie('cityInfo');
        $city     = ($cityInfo && isset($cityInfo['id'])) ? $cityInfo['id'] : 0;
        $keyword  = input('get.keyword');
        $where['city']  = $city;
        $keyword && $where['title'] = ['like',['%'.$keyword.'%']];
        $lists = model('second_house')->where($where)->field('id,title,price,address')->limit(10)->select();
        $return['status'] = 0;
        $lists = objToArray($lists);
        if($lists){
            foreach($lists as &$v){
                $v['url'] = url('Second/detail',['id'=>$v['id']]);
            }
            $return['status'] = 1;
            $return['data']   = $lists;
        }
        return json($return);
    }
}